// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CLOUDAPPENGINE_CAECONTROLCMDHANDLETHREAD_H
#define CLOUDAPPENGINE_CAECONTROLCMDHANDLETHREAD_H

#include <map>
#include "../cae_dataPipe/CaeDataPipe.h"
#include "../cae_common/CaeAesUtils.h"

class CaeControlCmdHandleThread : public CaeThread {
public:
    CaeDataPipe* m_cmdData;

    explicit CaeControlCmdHandleThread(CaeDataPipe* pipes);

    ~CaeControlCmdHandleThread() override;

    void Run() override;

private:
    AesInfo *m_aesInfo;

    bool CommandFilter(int commandType);

    void ResponseInvalidCmd(const int code, bool closeClient = false);

    void HandleStartCmd(std::map<std::string, std::string> &parameters);

    bool CheckAndStoreParams(std::map<std::string, std::string> &parameters);

    int QueryAesKeyAndVerifyData(std::map<std::string, std::string> &parameters);

    bool ParseStartInfo(std::string &encryptData, AesInfo *aesInfo);

    void HandleSetMediaConfigCmd(std::map<std::string, std::string> &parameters);

    void HandleReconnectCmd(std::map<std::string, std::string> &parameters);

    void HandleResumeCmd(std::map<std::string, std::string> &parameters);

    bool ResponseCmdToClient(std::map<std::string, std::string> params);

    char* ParseMediaConfig(std::string mediaConfig);

    bool Init();

    void Deinit() noexcept;

    void HandleCommand(int commandType, std::map<std::string, std::string> parameters);
};

#endif // CLOUDAPPENGINE_CAECONTROLCMDHANDLETHREAD_H
